#库
from openpyxl.styles import Alignment, PatternFill, Font
from openpyxl.utils import get_column_letter
from openpyxl.drawing.image import Image
import openpyxl
import calendar
from configobj import ConfigObj

#自定义
from configStructure import DateConfig, StyleConfig
from dataStructure import DayStructure, generateOneMonth

#################  start 全局数据段  ###############

#获取配置
global_config = ConfigObj('config.cfg')
#生成配置类
MyDateConfig = DateConfig(global_config)
MyStyleConfig = StyleConfig(global_config)
#生成数据
OneMonthDayDetailList = generateOneMonth(2021, 3, ConfigObj('data.cfg'))

#################  end 全局数据段  ###############

## 生成excel
# para: none
# return: none
def generateXlsx():
    #先去掉循环月逻辑
    #for i in range(1, 13):
        ## 添加工作表

    #全局变量显式指定
    global MyDateConfig
    global MyStyleConfig
    global OneMonthDayDetailList

    wb = openpyxl.Workbook()
    sheet = wb.create_sheet(index = 0, title = str(MyDateConfig.month) + '月')
    # 生成具体一个月的信息
    for rowIndex, rowContent in enumerate(OneMonthDayDetailList):
        for colIndex, colContent in enumerate(rowContent):
            if colContent.day == 0: #没有数据
                sheet.cell(row = rowIndex + 4, column = colIndex + 1).value = ""
            else:                   #有数据
                if colContent.content == "":
                    sheet.cell(row = rowIndex + 4, column = colIndex + 1).value = str(colContent.day)
                else:
                    sheet.cell(row = rowIndex + 4, column = colIndex + 1).value = str(colContent.day) + '\n' + colContent.content
                # 设置内容字体
                sheet.cell(row = rowIndex + 4, column = colIndex + 1).font = Font(
                    MyStyleConfig.content_font[0], 
                        size = MyStyleConfig.content_font[1]
                    )
                # 设置内容对齐
                sheet.cell(row = rowIndex + 4, column = colIndex + 1).alignment = Alignment(
                    horizontal = MyStyleConfig.content_day_horizontal, 
                    vertical = MyStyleConfig.content_day_vertical,
                    wrap_text = True    #自动换行
                )
    '''此处失效
    # 单元格填充色属性设置
    #fill = PatternFill("solid", fgColor="FFFFF0")
    # 对单元格进行颜色填充
    #for k1 in range(1, 100):
    #    for k2 in range(1, 100):
    #        sheet.cell(row=k1, column=k2).fill = fill
    '''

    # 添加星期几信息行
    for weekIndex, weekTitle in enumerate(['星期一', '星期二', '星期三', '星期四', '星期五', '星期六', '星期日']):
        sheet.cell(row = 3, column = weekIndex + 1).value = weekTitle
        # 设置星期字体
        sheet.cell(row = 3, column = weekIndex + 1).font = Font(
            MyStyleConfig.content_font[0], 
                size = MyStyleConfig.content_font[1]
        )
        # 设置星期对齐
        sheet.cell(row = 3, column = weekIndex + 1).alignment = Alignment(
            horizontal = MyStyleConfig.content_week_horizontal,
            vertical = MyStyleConfig.content_week_vertical
        )
        # 设置列宽12
        sheet.column_dimensions[get_column_letter(weekIndex + 1)].width = MyStyleConfig.content_column_width
    # 设置行高30
    for k4 in range(3, 9):
        sheet.row_dimensions[k4].height = MyStyleConfig.content_row_height

    '''
    # 合并单元格
    # sheet.merge_cells('I1:P20')
    # 添加图片
    #img = Image('PR2.jpg')

    #sheet.add_image(img, 'I1')
    '''

    # 添加年份及月份
    sheet.cell(row=1, column=1).value = str(MyDateConfig.year) + '年' + str(MyDateConfig.month) + '月'
    # 设置年份及月份文本属性
    sheet.cell(row=1, column=1).font = Font(
        MyStyleConfig.title_font[0],
            size=MyStyleConfig.title_font[1],
            bold=MyStyleConfig.title_font[2],
            color=MyStyleConfig.title_font[3]
        )
    #sheet.cell(row=4, column=1).font = Font(u'微软雅黑', size=16, bold=True, color='FF7887')
    sheet.cell(row=1, column=1).alignment = Alignment(
            horizontal = MyStyleConfig.title_horizontal,
            vertical = MyStyleConfig.title_vertical
        )
    #sheet.cell(row=4, column=1).alignment = align
    # 保存文档
    wb.save('20200920todo_calendar.xlsx')


## 入口函数
if __name__ == "__main__":
    generateXlsx()
    print("Done with generate todo_calendar")
